package recursion;

public class MiGong {
    public static void main(String[] args) {
        // 创建二维数组模拟迷宫
        int[][] map = new int[8][7];

        // 使用1表示墙
        for(int i = 0;i<7;i++){
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 0; i < 8; i++){
            map[i][0] = 1;
            map[i][6] = 1;
        }
        map[3][1] = 1;
        map[3][2] = 1;
        // 输出地图
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }
        findWay(map,1,1);
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }
    }

    /**
     * 用递归策略找路线，遵循的规则是下-右-上-左
     * 1表示墙，0表示没有走过，2表示已经走过，3表示该点走不通
     * @param map 地图
     * @param i i和j表示从地图的哪个位置开始找 i表示行 j表示列
     * @param j
     * @return
     */
    public static boolean findWay(int[][] map,int i ,int j){
        if(map[6][5] == 2){
            //说明最终路径已经找到
            return true;
        }else {
            if(map[i][j] == 0 ){
                //当前点还没有走过 那么就默认走他
                map[i][j] = 2;
                if(findWay(map,i+1,j)){
                    return true;
                }else if(findWay(map,i,j+1)){
                    return true;
                }else if(findWay(map,i-1,j)){
                    return true;
                }else if(findWay(map,i,j-1)){
                    return true;
                }else {
                    map[i][j] = 3;
                    return false;
                }
            }else {
                //说明为1、2、3
                return false;
            }
        }
    }
}
